import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/authStore'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/AboutSection')
  },
  {
    path: '/services',
    name: 'Services',
    component: () => import('@/views/ServicesGrid')
  },
  {
    path: '/1',
    name: 'AboutPage',
    component: () => import('@/views/service/AboutPage')
  },
  {
    path: '/2',
    name: 'ServicePage',
    component: () => import('@/views/service/ServicePage')
  },
  {
    path: '/3',
    name: 'ProjectPage',
    component: () => import('@/views/service/ProjectPage')
  },
  {
    path: '/ResumeCreation',
    name: 'ResumeCreation',
    component: () => import('@/views/service/ResumeCreation/ResumeCreation')
  },
  {
    path: '/FillIn',
    name: 'FillInTheEmploymentCompany',
    component: () => import('@/views/service/EmploymentHelp/FillInTheEmploymentCompany')
  },
  {
    path: '/AuthPage',
    name: 'AuthPage',
    component: () => import('@/views/AuthPage')
  },
  {
    path: '/xingce',
    name: 'xingce',
    component: () => import('@/views/service/QuestionBank/xingce')
  },
  {
    path: '/zhiye',
    name: 'zhiye',
    component: () => import('@/views/service/QuestionBank/zhiye')
  },
  {
    path: '/zhuanye',
    name: 'zhuanye',
    component: () => import('@/views/service/QuestionBank/zhuanye')
  },
  {
    path: '/AISimulation',
    name: 'AISimulation',
    component: () => import('@/views/service/AiAssist/AISimulation')
  },
  {
    path: '/analyse',
    name: 'analyse',
    component: () => import('@/views/service/AiAssist/analyse')
  },
  {
    path: '/report',
    name: 'report',
    component: () => import('@/views/service/AiAssist/report')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  // 每次路由变化时检查登录状态
  authStore.init()
  // 检查是否需要登录
  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } 
  // 检查是否仅允许未登录用户访问
  else if (to.meta.guestOnly && authStore.isLoggedIn) {
    next({ name: 'Home' })
  } 
  // 其他情况正常放行
  else {
    next()
  }
})
export default router
